#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "student.h"
#define MAXSIZE 1024
#define SIZE 1024
void display();
void exam();
void now_time();
void test_message();

// 考试信息
void test_message()
{
    char class[] = "C语言";
    char test_time[] = "2024 06月07日 9:00";
    printf("考试科目：%s\n考试时间：%s\n", class, test_time);
}

// 界面显示
void display()
{
    char input[50];
    int count = 40;
    for (int i = 0; i < count; i++)
    {
        printf("-");
    }
    printf("\n");
    printf("**欢迎来到高级语言程序设计课程管理系统**\n");
    printf("**************学生在线考试**************\n");

    for (int i = 0; i < count; i++)
    {
        printf("-");
    }
    printf("\n");
    test_message();
    printf("请输入start，进入考试\n");
    scanf("%s", input);
    if (strcmp(input, "start") == 0)
    {
        now_time();
    }
    else
    {
        printf("无效输入，请输入start进行考试\n");
        display();
    }
}

// 题库读取与答案输入
void exam()
{
    FILE *fp_test, *fp_answer;
    char line[MAXSIZE];
    char answer[SIZE];
    char input_prompt[] = "请输入您的答案（）";
    char input_prompt1[] = "请输入您的程序（）";
    char *prompt_end;
    char answerFile[] = "answer.txt";

    fp_test = fopen("test.txt", "r");
    if (fp_test == NULL)
    {
        perror("Error opening test file");
        return;
    }

    fp_answer = fopen(answerFile, "w");
    if (fp_answer == NULL)
    {
        perror("Error opening answer file");
        fclose(fp_test);
        return;
    }

    while (fgets(line, MAXSIZE, fp_test))
    {
        line[strcspn(line, "\n")] = 0;

        prompt_end = strchr(input_prompt, ')');
        if (prompt_end)
            *prompt_end = 0;
        if (strcmp(line, input_prompt) == 0)
        {
            printf("> ");
            if (fgets(answer, SIZE, stdin))
            {
                answer[strcspn(answer, "\n")] = 0;
                fputs(answer, fp_answer);
                fputc('\n', fp_answer);
            }
        }
        else if (strcmp(line, input_prompt1) == 0)
        {
            printf("> ");

            while (fgets(answer, SIZE, stdin) && strcmp(answer, "\n") != 0)
            {
                fputs(answer, fp_answer);
            }
        }
        else
        {
            printf("%s\n", line);
        }
    }

    fclose(fp_test);
    fclose(fp_answer);
}

// 获取当前时间开始考试
void now_time()
{
    time_t currentTime;
    struct tm *localTimeStruct;

    currentTime = time(NULL);
    localTimeStruct = localtime(&currentTime);

    printf("当前时间是： %02d时%02d分%02d秒\n",
           localTimeStruct->tm_hour,
           localTimeStruct->tm_min,
           localTimeStruct->tm_sec);

    if ((localTimeStruct->tm_hour >= 9 && localTimeStruct->tm_hour < 11) ||
        (localTimeStruct->tm_hour == 9 && localTimeStruct->tm_min > 0) ||
        (localTimeStruct->tm_hour == 11 && localTimeStruct->tm_min == 0 && localTimeStruct->tm_sec == 0))
    {
        printf("考试开始，请开始作答\n");
        exam();
    }
    else
    {
        printf("考试时间未到，不允许进入\n");
        test_message();
        printf("请在规定时间内输入start,方可开始考试\n");
        display();
    }

    return 0;
}

int main()
{
    display();
}